﻿@model FileUploadDemoForMVC.Models.BlogModel

@{
    ViewBag.Title = "Index";
}
<script src="~/Content/jquery-1.10.2.js"></script>
<h2>Index</h2>
@using (Html.BeginForm("index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="上传" />
    if (ViewBag.msg != null)
    {
        <strong style="color: red">@ViewBag.msg.ToString()</strong>
    }
}


<hr />

<h2>UploadFile</h2>
@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        @Html.LabelFor(model => model.BlogName, htmlAttributes: new { @class = "control-label col-md-2" })
        @Html.DisplayTextFor(b => b.BlogName)
        <div class="col-md-10">
            @Html.EditorFor(model => model.BlogName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.BlogName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.BlogAddress, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.BlogAddress, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.BlogAddress, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.BlogPhoto, htmlAttributes: new { @class = "control-label col-md-2" })
        @Html.DisplayTextFor(b => b.BlogPhoto)
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.BlogPhoto, new { type = "file" })
            @Html.ValidationMessageFor(model => model.BlogPhoto, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="上传" class="btn btn-default" id="uploadFileSub"/>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-12">
            @if (ViewBag.msg2 != null)
            {
                <strong style="color: red">@ViewBag.msg2.ToString()</strong>
            }
        </div>

    </div>


    <script type="text/javascript">
        function GetFileSize(fileid) {
            var fileSize = 0;
            fileSize = $("#" + fileid)[0].files[0].size;
            fileSize = fileSize / 1048576;
            return fileSize;
        }
        function getNameFromPath(strFilepath) {
            var objRE = new RegExp(/([^\/\\]+)$/);
            var strName = objRE.exec(strFilepath);

            if (strName == null) {
                return null;
            }
            else {
                return strName[0];
            }
        }
        $("#BlogPhoto").change(function () {
            var file = getNameFromPath($(this).val());
            if (file != null) {
                var errors = $(document).find(".field-validation-error");
                $.each(errors, function (k, v) {
                    if ($(v).attr("data-valmsg-for") === "BlogPhoto") {
                        $(v).hide();
                    }
                });
                var extension = file.substr((file.lastIndexOf('.') + 1));
                switch (extension) {
                case 'jpg':
                case 'png':
                case 'gif':
                case 'pdf':
                    fileTypeBool = false;
                    break;
                default:
                    fileTypeBool = true;
                }
            }
            if (fileTypeBool) {
                $("#warning").html("只能上传扩展名为jpg,png,gif,pdf的文件！");
                return false;
            }
            else {
                var size = GetFileSize('BlogPhoto');
                if (size > 4) {
                    fileSizeBool = true;
                    $("#warning").html("上传文件已经超过4兆！");
                } else {
                    fileSizeBool = false;
                }
            }
        });

        $("#uploadFileSub").submit(function () {
            $("input[type='text']").each(function (k, v) {
                if ($(v).length) {
                    $(v).siblings("span").hide();
                }
            });
            if (fileTypeBool || fileSizeBool) {
                return false;
            }
        });
    </script>
}